/************************************************************************************
* SongInfoDisplayWidget.cpp                                                         *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "SongInfoDisplayWidget.h"
#include "common/Utils.h"
#include "common/SizeConstants.h"
#include "collector/CollectorItem.h"

#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScrollArea>

SongInfoDisplayWidget::SongInfoDisplayWidget(QWidget *parent) :
    QWidget(parent)
{
    m_scroller = new SongScroller(this);
    m_timeLabel = new QLabel(this);
    m_bitrateLabel = new QLabel(this);
    m_freqLabel = new QLabel(this);
    m_channelLabel = new QLabel(this);
    m_cover = new QLabel(this);
    m_currDisp = Title;
    m_starWidget = new StarWidget(None, this);
    setupUi();

//    m_scroller->setObjectName("yampcomp");
//    m_timeLabel->setObjectName("yampcomp");
//    m_bitrateLabel->setObjectName("yampcomp");
//    m_freqLabel->setObjectName("yampcomp");
//    m_channelLabel->setObjectName("yampcomp");
//    m_cover->setObjectName("yampcomp");
//    this->setObjectName("songinfo");

}


void SongInfoDisplayWidget::reset() {
    m_timeLabel->setText("");
    m_bitrateLabel->setText("");
    m_freqLabel->setText("");
    m_channelLabel->setText("");
    m_scroller->stop();
    m_cover->clear();
    m_starWidget->hide();
}


void SongInfoDisplayWidget::setupUi() {
    setAutoFillBackground(true);
    QPalette textPal;
    textPal.setColor(QPalette::Window, Qt::transparent);
    textPal.setColor(QPalette::WindowText, Qt::lightGray);


    QHBoxLayout* mainLayout = new QHBoxLayout();
    mainLayout->addWidget(m_cover);

    QVBoxLayout* firstLayout = new QVBoxLayout();
    QHBoxLayout* horLayout = new QHBoxLayout();
    horLayout->addWidget(m_scroller, 0, Qt::AlignRight);
    horLayout->addWidget(new QLabel("  "));
    firstLayout->addLayout(horLayout);

    m_timeLabel->setAutoFillBackground(true);
    m_timeLabel->setFont(QFont("Monospace", 11, 3, false)); //<<Set>>
    m_timeLabel->setPalette(textPal);

    firstLayout->addWidget(m_starWidget, 0, Qt::AlignCenter);
    firstLayout->addWidget(m_timeLabel, 0, Qt::AlignCenter);
    m_starWidget->hide();

    m_bitrateLabel->setAutoFillBackground(true);
    m_bitrateLabel->setFont(QFont("Monospace", 8)); //<<Set>>
    m_bitrateLabel->setPalette(textPal);

    m_freqLabel->setAutoFillBackground(true);
    m_freqLabel->setFont(QFont("Monospace", 8)); //<<Set>>
    m_freqLabel->setPalette(textPal);

    m_channelLabel->setAutoFillBackground(true);
    m_channelLabel->setFont(QFont("Monospace", 8)); //<<Set>>
    m_channelLabel->setPalette(textPal);

    QHBoxLayout* bottomLayout = new QHBoxLayout();
    bottomLayout->addWidget(m_bitrateLabel, 0, Qt::AlignCenter);
    bottomLayout->addWidget(m_freqLabel, 0, Qt::AlignCenter);
    bottomLayout->addWidget(m_channelLabel, 0, Qt::AlignCenter);
    firstLayout->addLayout(bottomLayout, 10);

    mainLayout->addLayout(firstLayout);
    mainLayout->setMargin(0);
    this->setLayout(mainLayout);

    setMaximumSize(FIRST_SPLITTER_WIDTH,
                   PLAYER_WIDGET_HEIGHT - 100);
    setMinimumSize(FIRST_SPLITTER_WIDTH,
                   PLAYER_WIDGET_HEIGHT - 100);

    connect(m_starWidget, SIGNAL(ratingSet(MediaRating)),
            this, SIGNAL(ratingChanged(MediaRating)));
}


void SongInfoDisplayWidget::setItem(MediaItem *item) {
    m_item = item;
    refresh();

}


void SongInfoDisplayWidget::refresh() {
    if(m_item ->itemType() == CollectorItem::ITEM_TYPE) {
        CollectorItem* colItem = (CollectorItem*) m_item ;
        m_starWidget->setRating(colItem->rating());
        m_starWidget->show();
    }
    else {
        m_starWidget->hide();
    }

    m_scroller->setCurrentItem(m_item);
    m_timeLabel->setText(QString("00:00 | %1 |-%1").
                        arg(Utils::getStringTime(m_item->duration())));
    m_bitrateLabel->setText(QString("%1 Kbps").arg(m_item->bitrate()));
    m_freqLabel->setText((QString("%1 KHz").arg(m_item->sampleRate() / 1000)));
    m_channelLabel->setText(QString("%1Ch").arg(m_item->numChannels()));
    QPixmap pixmap;
    QImage* image = Utils::fetchCover(m_item->source()->fileName());
    pixmap.convertFromImage(*image);
    delete image;
    m_cover->setPixmap(pixmap.scaled(QSize(100, this->geometry().height())));
}




void SongInfoDisplayWidget::tick(qint64 timeMs) {
        int elapsedSec = timeMs / 1000;
        int remSec     = m_item->duration() - timeMs / 1000;
        m_timeLabel->setText(QString("%1 | %2 |-%3").
                            arg(Utils::getStringTime(elapsedSec)).
                            arg(Utils::getStringTime(m_item->duration())).
                            arg(Utils::getStringTime(remSec)));
}



//int main(){
//    static const char *IdPicture = "APIC" ;
//    TagLib::MPEG::File mpegFile("\\Storage Card\\YeDooriyan.mp3");
//    TagLib::ID3v2::Tag *id3v2tag = mpegFile.ID3v2Tag();
//    TagLib::ID3v2::FrameList Frame ;
//    TagLib::ID3v2::AttachedPictureFrame *PicFrame ;
//    void *RetImage = NULL, *SrcImage ;
//    unsigned long Size ;
//    FILE *jpegFile;
//    fopen_s(&jpegFile,"FromId3.jpg","wb");
//    if ( id3v2tag )  {    // picture frame
//        Frame = id3v2tag->frameListMap()[IdPicture] ;
//        if (!Frame.isEmpty() )     {      // find cover art
//            for(TagLib::ID3v2::FrameList::ConstIterator it = Frame.begin(); it !=Frame.end(); ++it)
//            {

//                PicFrame = (TagLib::ID3v2::AttachedPictureFrame *)(*it) ;
//                if ( PicFrame->type() == TagLib::ID3v2::AttachedPictureFrame::FrontCover) {
//                    // extract image (in it's compressed form)
//                    Size = PicFrame->picture().size() ;
//                    SrcImage = malloc ( Size ) ;
//                    if ( SrcImage ) {
//                        memcpy ( SrcImage, PicFrame->picture().data(), Size ) ;
//                        fwrite(SrcImage,Size,1, jpegFile);
//                        fclose(jpegFile);
//                        free( SrcImage ) ;
//                    }
//                    if ( RetImage ) {
//                        break ;
//                    }
//                }
//            }
//        }
//    }
//    return(0);
//}
